package com.example.springbootdemo2023.bus.menu.controller;


import com.example.springbootdemo2023.bus.menu.dto.Menu;
import com.example.springbootdemo2023.bus.menu.service.IMenuService;
import com.example.springbootdemo2023.core.annotations.MySysLog;
import com.example.springbootdemo2023.core.annotations.MySysLogOpration;
import com.example.springbootdemo2023.core.dto.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 菜单管理功能
 *
 * @author GaoJingBo
 * @since 2023-03-27
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags={"菜单管理操作接口"})
@Slf4j
public class MenuController {

    @Resource(name="menuServiceImpl")
    private IMenuService menuService;


    /**
     * 菜单管理：获取菜单列表
     * @param menu  查询对象
     * @return
     */
    @GetMapping("/getList")
    @ApiOperation("获取菜单列表")
    @MySysLog(modelName="菜单管理",methodDesc = "获取菜单列表",methodType = MySysLogOpration.GET)
    public MyResult getMenuList(Menu menu){

        log.info("--- 菜单管理：获取菜单列表");

        MyResult result = new MyResult();

        try{
            result = menuService.getList(menu);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("获取菜单列表异常");
            result.setCode("9999");
        }

        return result;

    }


    /**
     * 菜单管理：新增菜单
     * @param menu 添加的菜单数据
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增菜单")
    @MySysLog(modelName="菜单管理",methodDesc = "新增菜单",methodType = MySysLogOpration.POST)
    public MyResult addMenu(@RequestBody Menu menu){

        log.info("--- 菜单管理：新增菜单");

        MyResult result = new MyResult();

        try{
            result = menuService.addMenu(menu);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("新增菜单异常");
            result.setCode("9999");
        }
        return result;
    }

    /**
     * 菜单管理：删除菜单
     * @param innerCode  要删除的父级innerCode
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除菜单")
    @MySysLog(modelName="菜单管理",methodDesc = "删除菜单",methodType = MySysLogOpration.DELETE)
    public MyResult deleteMenu(String innerCode){
        log.info("--- 菜单管理：删除菜单");

        MyResult result = new MyResult();

        try{
            result = menuService.deleteMenu(innerCode);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除菜单异常");
            result.setCode("9999");
        }
        return result;
    }

    /**
     * 菜单管理：更新菜单
     * @param menu
     * @returnbill
     */
    @PutMapping("/update")
    @ApiOperation("更新菜单")
    @MySysLog(modelName="菜单管理",methodDesc = "更新菜单",methodType = MySysLogOpration.PUT)
    public MyResult updateMenu(@RequestBody Menu menu){
        log.info("--- 菜单管理：更新菜单");

        MyResult result = new MyResult();

        try{
            result = menuService.updateMenu(menu);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("更新菜单异常");
            result.setCode("9999");
        }
        return result;
    }

    /**
     * 菜单管理：获取菜单信息通过id
     * @param menuID 菜单id
     * @return
     */
    @GetMapping("/getByID")
    @ApiOperation("获取菜单信息通过id")
    @MySysLog(modelName="菜单管理",methodDesc = "获取菜单信息通过id")
    public MyResult getMenuDataByID(String menuID){
        log.info("--- 菜单管理：获取菜单信息通过id");

        MyResult result = new MyResult();

        try{
            result = menuService.getMenuDataByID(menuID);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("获取菜单信息通过id异常");
            result.setCode("9999");
        }
        return result;
    }
}
